package com.aglhz.nature.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.modle.UserInfo;
import com.aglhz.nature.view.LoginActivity;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class CheckTokenHelper {
	private static UserInfo userInfo;
	private static int code;
	private static String TAG = CheckTokenHelper.class.getName();
	private static SharedPreferences sharedPreferences;

	private static String wx_token;
	private static String nor_token;

	// 检测登录接口
	public static void loginCheck(final Context context, final String token) {
		// token=getToken(context);
		String url = ServerAPI.APIHEARD + ServerAPI.LOGINCHECK + "?token="
				+ token;
		Log.e("loginCheck", "--------" + url);
		AsyncHttpClient client = AsyncHttpCilentUtil.getInstance(context);
		client.post(url, new TextHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response LogChecked" + responseString);

					Gson gson = new Gson();

					UserInfo uInfo = gson.fromJson(responseString,
							UserInfo.class);
					userInfo = (UserInfo) uInfo.clone();
					code = Integer.parseInt(userInfo.getOther().getCode());
					if (!(code == 200)) {
						// 获取sharedPreferences里面的信息,微信
						sharedPreferences = context.getSharedPreferences(
								"WX_INFO", Context.MODE_PRIVATE);
						wx_token = sharedPreferences.getString("token", "");
						// 获取sharedPreferences里面的信息,普通登陆
						sharedPreferences = context.getSharedPreferences(
								"Normal_Info", Context.MODE_PRIVATE);
						nor_token = sharedPreferences.getString("NORMAL_Token",
								"");
						if (token == wx_token) {
							context.getSharedPreferences("WX_INFO",
									Context.MODE_PRIVATE).edit().clear()
									.commit();
						} else {
							context.getSharedPreferences("Normal_Info",
									Context.MODE_PRIVATE).edit().clear()
									.commit();
						}

					} else {

					}

					Log.e(TAG, "success" + "ddddddddddddddddddsuccess");
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

				Log.e(TAG, "failure");
			}

		});

	}

	
}
